import com.itheima.Student;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

//案例：集合到文件（数据排序改进版）
//需求： 键盘录入5个学生信息（姓名，语文成绩，数学成绩，英语成绩）。要求按照成绩总分从高到低写入文本文件。
//格式：姓名，语文成绩，数学成绩，英语成绩
public class TreeSet2File {
    public static void main(String[] args) throws IOException {
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num1 = s2.getSum() - s1.getSum();
                int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese():num1;
                int num3 = num2 == 0 ? s2.getMath() - s1.getMath():num2;
                int num4 = num3 == 0 ? s2.getEnglish() - s1.getEnglish():num3;
                return num4 ;
            }
        });


        for(int i = 0; i < 5; i++){
            Scanner sc = new Scanner(System.in);
            System.out.println("--------");
            System.out.println("Please input the info of student " + (i + 1) + " ~ ");
            System.out.println("The name of student " + (i + 1) + " is : ");
            String name = sc.nextLine();
            System.out.println("The Chinese score of student " + (i + 1) + " is : ");
            int  chinese = sc.nextInt();
            System.out.println("The Math score of student " + (i + 1) + " is : ");
            int  math = sc.nextInt();
            System.out.println("The English score of student " + (i + 1) + " is : ");
            int  english = sc.nextInt();
            ts.add(new Student(name,chinese,math,english));
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("MyFile\\src\\tmp\\TreeSet2File.txt"));
        for(Student stu : ts){
            StringBuilder sb = new StringBuilder();
            sb.append(stu.getName()).append(" ").append(stu.getChinese()).append(" ").append(stu.getMath()).append(" ").append(stu.getEnglish()).append(" ").append(stu.getSum());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
